/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions.Fitters;

import JFreeChartExtensions.Series.XYExtendedIntervalSeries;
import JFreeChartExtensions.Series.XYRegressionSeries;
import org.apache.commons.math.optimization.OptimizationException;
import org.apache.commons.math.optimization.fitting.HarmonicFitter;
import org.apache.commons.math.optimization.fitting.HarmonicFunction;
import org.apache.commons.math.optimization.general.LevenbergMarquardtOptimizer;

/**
 *
 * @author Nobody
 */
public class HarmonicReggresion implements IReggression {

    public int PRECISE = 100;

    private String lastFunctionFormula;
    @Override
    public XYRegressionSeries regress(XYExtendedIntervalSeries series) throws OptimizationException {
        XYRegressionSeries regr = null;
      
            LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
            HarmonicFitter fitter = new HarmonicFitter(optimizer);

            for (int i = 0; i < series.getItemCount(); ++i) {
                fitter.addObservedPoint(1, (Double) series.getX(i), (Double) series.getYValue(i));
            }
            HarmonicFunction harmonicFunction = fitter.fit();
            lastFunctionFormula = harmonicFunction.toString();
            regr = new XYRegressionSeries(lastFunctionFormula);
            for (double x = series.getMinX(); x < series.getMaxX(); x += (series.getMaxX() - series.getMinX()) / PRECISE) {
                regr.add(x, harmonicFunction.value(x));
            }

      

        return regr;
    }

    @Override
    public String toString() {
        return "HarmonicReggresion";
    }

}
